<template>
    <div class="app-container">
   <!--查询表单-->
<div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="课程名称">
              <el-input style="width: 100%" v-model="searchObj.name" placeholder="课程名称"></el-input>
            </el-form-item>
          </el-col>
        <el-col :span="6">
            <el-form-item label="所属分类">
              <template>
                <el-select
                  v-model="searchObj.typeId"
                  placeholder="请选择"
                  style="margin-right: 10px; width: 80%"
                >
                  <el-option
                    v-for="item in courseTypeList"
                    :key="item.id"
                    :label="item.typeName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" icon="el-icon-search" size="mini"  @click="fetchPageList()">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>
        
            <!-- 工具条 -->
<div class="tools-div">
  <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
</div>

<div class="app-container">
  <!-- 列表 -->
  <el-table
        v-loading="listLoading"
        :data="list"
        stripe
        border
        @selection-change="handleSelectionChange"
        style="width: 100%; margin-top: 10px"
      >
        <el-table-column type="selection" />

        <el-table-column label="序号" width="70" align="center">
          <template slot-scope="scope">
            {{ (page - 1) * limit + scope.$index + 1 }}
          </template>
        </el-table-column>
      
      <!-- <el-table-column prop="param.typeName" label="分类" /> -->
      <el-table-column prop="name" label="课程名称" /> 
      <el-table-column prop="param.username" label="老师"/>
      <el-table-column prop="special" label="特色"/>
      <el-table-column prop="duration" label="课时"/>
      <el-table-column prop="attendCount" label="当前人数"/>
      <el-table-column prop="total" label="总人数"/>
      <el-table-column label="是否审核" width="80">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status === 1"
              @change="switchStatus(scope.row)"
            >
            </el-switch>
          </template>
      </el-table-column>
      <el-table-column label="是否推荐" width="80">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.recommend === 1"
              @change="switchRecommend(scope.row)"
            >
            </el-switch>
          </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
       <!-- <el-table-column prop="updateTime" label="修改时间" width="200"/> -->
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
              type="warning"
              icon="el-icon-baseball"
              size="mini"
              @click="showPlayPage(scope.row)"
              title="播放视频"
            />
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)" title="修改"/>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除"/>

        </template>
      </el-table-column>
    </el-table>
</div>

<el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%" >
      <el-form ref="dataForm" :model="sysCourse" label-width="150px" size="small" style="padding-right: 40px;">
        <el-form-item label="所属分类" prop="courseType">
            <el-select v-model="sysCourse.courseType"  placeholder="请选择影视分类">
              <el-option
                v-for="item in courseTypeList"
                :key="item.id"
                :label="item.typeName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        <el-form-item label="课程名称">
          <el-input v-model="sysCourse.name"/>
        </el-form-item>
        <el-form-item label="教师" prop="teacherId">
          <el-select v-model="sysCourse.teacherId"  placeholder="请选择教师">
              <el-option
                v-for="item in teacherList"
                :key="item.id"
                :label="item.username"
                :value="item.id"
              ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="特色">
          <el-input v-model="sysCourse.special"/>
        </el-form-item>
        <el-form-item label="课时">
          <el-input v-model="sysCourse.duration"/>
        </el-form-item>
        <el-form-item label="当前人数">
          <el-input v-model="sysCourse.attendCount"/>
        </el-form-item>
        <el-form-item label="总人数">
          <el-input v-model="sysCourse.total"/>
        </el-form-item>
        <el-form-item label="影视文件" prop="video">
            <el-upload
              class="upload-demo"
              name="uploadVideo"
              action="http://localhost:8085/admin/system/upload/uploadVideo"
              :limit="1"
              :on-success="handleVideoSuccess"
              :before-upload="handleBeforeUpload"
              :show-file-list="false"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
</el-dialog>


      <!-- 分页组件 -->
  <el-pagination
    :current-page="page"
    :total="total"
    :page-size="limit"
    style="padding: 30px 0; text-align: center;"
    layout="total, prev, pager, next, jumper"
    @current-change="fetchPageList"
  />


    </div>
</template>

<script>

// 导入 
import api from '@/api/sysCourse/course.js'
import userApi from '@/api/user/user.js'
import couseTypeApi from "@/api/sysCourseType/courseType.js"


export default {
   data(){
    return{
        list:[],
        page:1,
        limit:5,
        tota:0,
        searchObj:{},
        listLoading:true,
        dialogVisible:false,
        sysCourse:{},
        courseTypeList:[],//课程分类
        teacherList:[],//老师

    }
   },
   created () {
    //this.listLoading=true;
     this.fetchPageList();
     this.fetchCourseTpyeList();
     this.fetchTeacherList();
   },
   methods:{

    //文件开始上传,开始屏幕遮罩
    handleBeforeUpload() {
        this.loading = true;
      },
    // 视频上传
    handleVideoSuccess(res, file) {
         this.loading = false;

         console.log(file.response);
        if (file.response != "") {
          this.sysCourse.playId = file.response.data;
          this.$message({
            type: "info",
            message: "视频上传成功",
            duration: 6000,
          });
        } else {
          this.$message({
            type: "info",
            message: "视频上传失败",
            duration: 6000,
          });
        }
      },

    // 播放视频
    showPlayPage(row) {
        this.$router.push('/assignVideo?id='+row.id);
  },

   // 查询所有的老师
   fetchTeacherList() {
      userApi.findTeacher().then((res) => {
        this.teacherList = res.data;
      });
    },

    // 查询课程分类
    fetchCourseTpyeList() {
      couseTypeApi.findAll().then((res) => {
        this.courseTypeList = res.data;
      });
    },
    //是否推荐
    switchRecommend(row) {
      // 如果 可用  设置为 不可用  如果不可用  设置为 可用
      row.recommend = row.recommend === 1 ? 0 : 1;
      api.updateRecommend(row.id, row.recommend).then((response) => {
        //a.给出提示
        this.$message.success(response.message || "操作成功");
        //b.再次刷新列表
        this.fetchData();
      });
    },

       // 修改状态
    switchStatus(row) {
      // 如果 可用  设置为 不可用  如果不可用  设置为 可用
      row.status = row.status === 1 ? 0 : 1;
      api.updateStatus(row.id, row.status).then((response) => {
        //a.给出提示
        this.$message.success(response.message || "操作成功");
        //b.再次刷新列表
        this.fetchData();
      });
    },
    edit(id){
      //1.弹框
      this.dialogVisible=true;
      //2.赋值
      api.findCourseById(id)
      .then(response=>{
        this.sysCourse = response.data;
      })
    },
    saveOrUpdate(){
       if(this.sysCourse.id!=null)
       {
         this.updateCourse();
       }
       else
       {
        this.addCourse();
       }
    },
    addCourse(){
       api
       .addCourse(this.sysCourse)
       .then(response=>{
             this.$message({
                type: 'success',
                message: '添加成功!'
              });
           //关闭弹框
           this.dialogVisible=false;
           // 刷新列表
           this.fetchPageList();   

       })
    },
    updateCourse(){
       api
       .updateCourse(this.sysCourse)
       .then(response=>{
             this.$message({
                type: 'success',
                message: '修改成功!'
              });
           //关闭弹框
           this.dialogVisible=false;
           // 刷新列表
           this.fetchPageList();   

       })
    },
    add(){
      this.dialogVisible=true;
      this.sysCourse={};
    },
    removeDataById(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          api.removeCourseById(id).then(response=>{
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.fetchPageList();
          })

        })
    },
    resetData(){
     //1.清空搜索条件
     this.searchObj={};
     //2.再次刷新列表
     this.fetchPageList();
    },
     fetchPageList(page=1){
        this.page=page;
        api.getCoursePageInfo(this.page,this.limit,this.searchObj).then(response=>{
            this.listLoading=false;
            // console.log(response);
            this.list = response.data.records;
            this.total = response.data.total;
        })
     }
   }
}
</script>

<style lang="less" scoped>

</style>